/*
 * Copyright (c) 2016, www.zm0618.com All Rights Reserved..
 */

package com.ibetter.mr.grade.medic.score;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Iterator;

/**
 * Created by zhaojun on 2016/12/12.
 */
public class GradeScoreCounterReducer extends Reducer<Text,DoubleWritable,Text,FloatWritable>{
    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {

        Iterator<DoubleWritable> iter = values.iterator();
        double scoreDouble=0;
        while (iter.hasNext()){
            DoubleWritable scoreWritable = iter.next();
            scoreDouble+=scoreWritable.get();
        }

        BigDecimal bigDecimal=new BigDecimal(scoreDouble);
        bigDecimal.setScale(1,BigDecimal.ROUND_HALF_UP);

        String keyStr=key.toString();
        String[] idKeys = StringUtils.split(keyStr, ",");
        context.write(new Text(StringUtils.join(idKeys,"\t")),new FloatWritable(  bigDecimal.floatValue()));
    }
}
